Tutorial: Your first RESTful web service | 您所在的位置:网站首页 › idea structure web › Tutorial: Your first RESTful web service |
Tutorial: Your first RESTful web service This tutorial describes how to create a simple RESTful web service in IntelliJ IDEA and deploy it to the GlassFishTomcat application server. The service will output Hello, World! when you access a specific URL through the web browser or otherwise send a GET request to this URL. Use the switcher at the top of this page for instructions for a different application server. You will create a new Java Enterprise project, add the necessary Java code, tell IntelliJ IDEA where your GlassFishTomcat server is located, then use a run configuration to build the artifact, start the server, and deploy the artifact to it. Here is what you will need: IntelliJ IDEA UltimateJava Enterprise development is not supported in the free IntelliJ IDEA Community Edition. For more information, see IntelliJ IDEA Ultimate vs IntelliJ IDEA Community Edition Relevant bundled pluginsBy default, all necessary plugins are bundled and enabled in IntelliJ IDEA Ultimate. If something does not work, make sure that the following plugins are enabled: Jakarta EE Platform Jakarta EE: Application Servers Jakarta EE: Web/Servlets Jakarta EE: RESTful Web Services (JAX-RS) GlassFish Tomcat and TomEE For more information, see Install plugins. Java SE Development Kit (JDK) version 1.8 or laterYou can get the JDK directly from IntelliJ IDEA as described in Java Development Kit (JDK) or download and install it manually, for example: Oracle JDK or OpenJDK. GlassFishThe GlassFish application server version 4.0 or later. You can get the latest release from the official repository. The Web Profile subset should be enough for the purposes of this tutorial. This tutorial uses Oracle OpenJDK 17, Jakarta EE 9.1, and GlassFish 6.2.5. For more information about the compatibility between other GlassFish, Java, and Jakarta EE versions, see https://github.com/eclipse-ee4j/glassfish#compatibility. TomcatThe Tomcat application server version 7 or later. This tutorial uses JDK 17, Jakarta EE 10, and Tomcat 10.1. For more information about the compatibility between other Tomcat, Java, and Jakarta EE versions, see https://tomcat.apache.org/whichversion.html. Web browserYou will need a web browser to view your web application. Create a new Java Enterprise projectIntelliJ IDEA includes a dedicated wizard for creating Java Enterprise projects based on various Java EE and Jakarta EE implementations. In this tutorial, we will create a simple web application. From the main menu, select File | New | Project. In the New Project dialog, select Jakarta EE. Enter a name for your project: RestGlassfishHelloWorldRestTomcatHelloWorld. For this tutorial, use Oracle OpenJDK 17 as the project SDK and select the REST service template. Don't select or add an application server, we will do it later. Select Maven and JUnit. Click Next to continue. In the Version field, select Jakarta EE 10 because that's what Tomcat 10.1 used in this tutorial is compatible with. For Tomcat 9, select Java EE 8. For Tomcat 10, select Jakarta EE 9.1. In the Version field, select Jakarta EE 9.1 because that's what GlassFish 6.2.5 used in this tutorial is compatible with. For GlassFish 5, select the Java EE 8 specification. For GlassFish 7, select Jakarta EE 10. In the Dependencies list, select the following: Contexts and Dependency Injection (CDI) RESTful Web Services (JAX-RS) Servlet Eclipse Jersey Client Weld SE Click Create. Explore the default project structureIntelliJ IDEA creates a project with some boilerplate code that you can build and deploy successfully. Use the Project tool window to browse and open files in your project or press Ctrl+Shift+N and type the name of the file. pom.xml is the Project Object Model with Maven configuration information, including dependencies and plugins necessary for building the project. 4.0.0 com.example RestGlassfishHelloWorld 1.0-SNAPSHOT RestGlassfishHelloWorld war UTF-8 1.8 1.8 5.7.1 javax.enterprise cdi-api 2.0.SP1 provided javax.ws.rs javax.ws.rs-api 2.1.1 provided javax.servlet javax.servlet-api 4.0.1 provided org.junit.jupiter junit-jupiter-api ${junit.version} test org.junit.jupiter junit-jupiter-engine ${junit.version} test org.apache.maven.plugins maven-war-plugin 3.3.1 4.0.0 com.example RestTomcatHelloWorld 1.0-SNAPSHOT RestTomcatHelloWorld war UTF-8 1.8 1.8 5.7.1 javax.servlet javax.servlet-api 4.0.1 provided org.glassfish.jersey.containers jersey-container-servlet 2.34 org.glassfish.jersey.media jersey-media-json-jackson 2.34 org.glassfish.jersey.inject jersey-cdi2-se 2.34 org.jboss.weld.se weld-se-core 3.1.8.Final org.junit.jupiter junit-jupiter-api ${junit.version} test org.junit.jupiter junit-jupiter-engine ${junit.version} test org.apache.maven.plugins maven-war-plugin 3.3.1HelloResource.java is a root resource class, which uses the following JAX-RS annotations to implement the RESTful web service: The @Path annotation identifies the URI for accessing this resource, relative to the application root. The @GET annotation indicates that the hello() method will process HTTP GET requests to the specified URI. The @Produces annotation specifies the MIME media type that the method produces and returns. package com.example.RestGlassfishHelloWorld; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/hello-world") public class HelloResource { @GET @Produces("text/plain") public String hello() { return "Hello, World!"; } } package com.example.RestTomcatHelloWorld; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/hello-world") public class HelloResource { @GET @Produces("text/plain") public String hello() { return "Hello, World!"; } }HelloApplication.java is a subclass of javax.ws.rs.core.Application, which is used to configure the environment where the application runs REST resources defined in your resource classes. The @ApplicationPath annotation identifies the URL mapping for the application root (by default, it is set to /api). package com.example.RestGlassfishHelloWorld; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/api") public class HelloApplication extends Application { } package com.example.RestTomcatHelloWorld; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; @ApplicationPath("/api") public class HelloApplication extends Application { } Configure the application serverLet IntelliJ IDEA know where the GlassFishTomcat application server is located. Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Application Servers. Click and select Glassfish ServerTomcat. Specify the path to the GlassFishTomcat server install location. IntelliJ IDEA detects and sets the name and version appropriately. Create a run configurationIntelliJ IDEA needs a run configuration to build the artifacts and deploy them to your application server. From the main menu, select Run | Edit Configurations. In the Run/Debug Configurations dialog, click , expand the Glassfish ServerTomcat Server node, and select Local. Fix any warnings that appear at the bottom of the run configuration settings dialog. Most likely, you will need to fix the following: On the Server tab, set the Server Domain to domain1. On the Deployment tab, add the artifact that you want to deploy: RestGlassfishHelloWorld:war exploded RestTomcatHelloWorld:war exploded On the Server tab, set the URL to point to the root resource: http://localhost:8080/RestGlassfishHelloWorld-1.0-SNAPSHOT/api/hello-worldhttp://localhost:8080/RestTomcatHelloWorld_war_exploded/api/hello-worldClick OK to save the run configuration. To run the configuration, press Alt+Shift+F10 and select the created application server configuration. Alternatively, if you have your run configuration selected in the main toolbar at the top, you can press Shift+F10 to run it. This run configuration builds the artifacts, then starts the GlassFishTomcat server, and deploys the artifacts to the server. You should see the corresponding output in the Run tool window. Once this is done, IntelliJ IDEA opens the specified URL in your web browser. If not, try opening the URL yourself: http://localhost:8080/RestGlassfishHelloWorld-1.0-SNAPSHOT/api/hello-world If not, try opening the URL yourself: http://localhost:8080/RestTomcatHelloWorld_war_exploded/api/hello-world TroubleshootingCompatibility with Jakarta EEIf you get a 404 error, make sure you have selected the Jakarta EE specification version that is compatible with your version of GlassFish when creating the project. For more information, see the GlassFish version compatibility. Older IntelliJ IDEA versionsIf you are using IntelliJ IDEA version 2020.2.2 or earlier, the New Project wizard will not add all of the necessary dependencies required for Tomcat. In this case, open pom.xml and add the following dependencies: org.glassfish.jersey.media jersey-media-json-jackson 2.31 org.glassfish.jersey.inject jersey-hk2 2.31For example, in version 2020.2.3, the generated pom.xml looks like this: 4.0.0 com.example RestTomcatHelloWorld 1.0-SNAPSHOT RestTomcatHelloWorld war 1.8 1.8 5.6.2 javax.ws.rs javax.ws.rs-api 2.1.1 provided org.glassfish.jersey.containers jersey-container-servlet 2.31 org.glassfish.jersey.media jersey-media-json-jackson 2.31 org.glassfish.jersey.inject jersey-hk2 2.31 org.glassfish.jersey.core jersey-client 2.31 org.junit.jupiter junit-jupiter-api ${junit.version} test org.junit.jupiter junit-jupiter-engine ${junit.version} test org.apache.maven.plugins maven-war-plugin 3.3.0 Last modified: 20 February 2023 RESTful web services Get started with REST development |
CopyRight 2018-2019 实验室设备网 版权所有 |